package fragmentson;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;

import java.util.ArrayList;

import activity.SonActivity;
import bean.EntityForSimple;
import bean.MessageForSimpleList;
import fragmentson.backhandle.BackHandledFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import service.ApiUtil;
import util.adapter.DevicesAdapterDemo;

/**
 * 登录
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class DeviceListDemo extends BackHandledFragment {

	private View rootView;
	private ListView lv_devices;
	private DevicesAdapterDemo adapter;

	private ArrayList<EntityForSimple> mDatas = new ArrayList<>();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_devicelist, null);
		initView();
		initData();
		return rootView;
	}

	public void initData(){
		ApiUtil.getApiService().getDevices(DemoApplication.getToken()).enqueue(new Callback<MessageForSimpleList>() {
			@Override
			public void onResponse(Call<MessageForSimpleList> call, Response<MessageForSimpleList> response) {
				try {
					MessageForSimpleList entity = response.body();
					if(entity.getCode().equals("10008")){
						mDatas = entity.getData();
						adapter = new DevicesAdapterDemo(getActivity(),mDatas);
						lv_devices.setAdapter(adapter);
						lv_devices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
							@Override
							public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
								DemoApplication.setDeviceid(mDatas.get(position).getDeviceid());
								getActivity().finish();
							}
						});
					}else{
						Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(Call<MessageForSimpleList> call, Throwable t) {
				Toast.makeText(getActivity(),getResources().getString(R.string.error),Toast.LENGTH_LONG).show();
			}
		});
	}

	public void initView(){
		((TextView) rootView.findViewById(R.id.tv_title)).setText(R.string.title_mycar);
		rootView.findViewById(R.id.iv_add).setOnClickListener(listener);
		lv_devices = ((ListView) rootView.findViewById(R.id.lv_devices));
	}

	public static DeviceListDemo newInstance() {
		DeviceListDemo fragment = new DeviceListDemo();
		return fragment;
	}

	public View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()){
				case R.id.iv_add:
					((SonActivity) getActivity()).addFragment(AddDeviceDemo.newInstance());
					break;
			}
		}
	};


	@Override
	public boolean onBackPressed() {
		return false;
	}
	
}
